package LeetCode刷题.暑假刷的算法;

import java.util.LinkedList;

/**
 * @program: Java_Study
 * @author: Xiaofan
 * @createTime: 2021-07-19 08:42
 * @description: Functions of this class is 设计一个排序系统
 **/
public class 设计一个排序系统 {
    private static class User {
        int id;
        int rank;

        public int getId() {
            return id;
        }

        public User(int id) {
            this.id = id;
        }

        public void setId(int id) {
            this.id = id;
        }

        public int getRank() {
            return rank;
        }

        public void setRank(int rank) {
            this.rank = rank;
        }

        @Override
        public String toString() {
            return "其序号为："+id+"，其排名最新更新为"+rank;
        }
    }

    private static class Node {
        LinkedList<User> q = new LinkedList();
        public void updateRank() {
            int i=1;
            for(User j:q){
                j.setRank(i++);
            }
        }
        public void addQueue(User user){
            q.add(user);
            user.setRank(q.size());
        }
        public void pollQueue(){
            q.poll();
            updateRank();
        }
        public void delQueue(User user){//可以直接删除某个元素
            q.remove(user);
            updateRank();
        }
    }

    public static void main(String args[]) {
        Node node=new Node();
        User user1=new User(1);
        User user2=new User(2);
        User user3=new User(3);
        User user4=new User(4);
        User user5=new User(5);
        node.addQueue(user1);
        node.addQueue(user2);
        node.addQueue(user3);
        node.addQueue(user4);
        node.addQueue(user5);

        System.out.println(user5);
        node.delQueue(user3);
        node.pollQueue();
        System.out.println(user5);
    }
}